package com.xqls.controller;



import com.xqls.biz.RolePermissionKeyBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.RoleBean;
import com.xqls.biz.RoleBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/roleController")
public class RoleController {

	@Autowired private RoleBiz roleBiz;
	@Autowired private RolePermissionKeyBiz rolePermissionBiz;
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/role/roleList.vm
	 * @return String
	 */
	@RequestMapping("/toPage")
	public String toPage(){
		return "/role/roleList";
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/role/roleAdd.vm
	 * @return ModelAndView
	 */
	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage(){
		ModelAndView view=new ModelAndView("/role/roleAdd");
		List<RoleBean> roleList=roleBiz.getList();

		view.addObject("roleList", roleList);
		return view;
	}
	
	/**
	 * 访问src/mainwebapp/WEB_INF/views/velocity/templates/role/roleEdit.vm
	 * 将roleId传到目标页面中
	 * @param roleId
	 * @return ModelAndView
	 */
	@RequestMapping("/toEditPage/{roleId}")
	public ModelAndView toEditPage(@PathVariable String roleId){
		ModelAndView view=new ModelAndView("/role/roleEdit");
		view.addObject("role", this.roleBiz.getById(roleId));
		return view;
	}
	
	/**
	 * 增加角色信息
	 * @param bean
	 * @return JSON
	 */
	@RequestMapping(value="/insertRecord",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String insertRecord(@RequestBody RoleBean bean){
		CommonResult result = this.roleBiz.insertRecord(bean);
		return JSONObject.toJSONString(result);
	}
	
	/**
	 * 修改角色信息
	 * @param bean
	 * @return JSON
	 */
	@RequestMapping(value="/updateRecord",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String updateRecord(@RequestBody RoleBean bean){
		CommonResult result = this.roleBiz.updateRecord(bean);
		return JSONObject.toJSONString(result);
	}
	
	/**
	 * 删除用户信息
	 * @param roleId
	 * @return JSON
	 */
	@RequestMapping(value="/deleteRecord/{roleId}",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteRecord(@PathVariable String roleId){
		CommonResult result1 =this.rolePermissionBiz.delRolePerByRoleId(roleId);
		CommonResult result = this.roleBiz.deleteRecord(roleId);


		return JSONObject.toJSONString(result);
	}
	
	/**
	 * 分页显示角色信息
	 * @param commonPage
	 * @return CommonResult
	 */
	@RequestMapping(value="/getList",produces="application/json;charset=UTF-8")
	@ResponseBody
	public CommonResult getList(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		CommonResult result = this.roleBiz.getListByPage(commonPage,request,response);
		if (result.getIsSuccess()) {
			result.setData(commonPage);
		}
		return result;
	}
}
